package com.hrt.web.addressBook.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.AddressBook;
import com.hrt.web.addressBook.service.AddressBookService;
@Controller
@RequestMapping("HRTWEB/address")
public class AddressBookController extends BaseController<AddressBook>{
	@Autowired
	private AddressBookService addressBookService;
	/**
	 * 保存
	 * @param addressBook
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("save")
	public void save(@ModelAttribute("addressInfor") AddressBook addressBook,HttpServletResponse reponse,
			HttpServletRequest request){
		String temp = request.getParameter("temp");
		
		if(temp.equals("on")){
			addressBook.setIsdefault(1);
		}
		else{
			addressBook.setIsdefault(0);
		}
		
		addressBookService.saveAddressBook(addressBook);
	}
	/**
	 * 更新
	 * @param addressBook
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("update")
	public void update(@ModelAttribute("addressInfor") AddressBook addressBook,HttpServletResponse reponse,
			HttpServletRequest request){
		addressBookService.updateAddressBook(addressBook);
	}
	/**
	 * 删除
	 * @param addressId
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("delete")
	public void delete(@RequestParam(value = "addressId",required = true) Long addressId,HttpServletResponse reponse,
			HttpServletRequest request){
		addressBookService.deleteAddressBook(addressId);
	}
	/**
	 * 显示
	 * @param memberId
	 * @param reponse
	 * @param request
	 */
	@RequestMapping("show")
	public ModelAndView show(@RequestParam(value = "memberId",required = true) Long memberId,HttpServletResponse reponse,
			HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		view.addObject("memberId", memberId);
		view.setViewName("/HRTWEB/address/show");
		return view;
	}
	/**
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("getAddress")
	@ResponseBody
	public Map getAddress(@RequestParam(value = "id",required = true) Long addressId,HttpServletRequest request){
		Map map = new HashMap();
		AddressBook addressBook = addressBookService.getAddById(addressId);
		map.put("addressBook", addressBook);
		return map;
	}
	
}
